from utils import json_util, macd_util_v1, form_util, date_util


# 大众评分 只有加分项目 没有不做评分 根据触发的时间长短评分
def common_exam(data):
    data['score'] = 0
    data['total_score'] = 0
    data['score_info'] = {'score': 0, 'msg': '', 'exam_time': date_util.get_timestamp_now()}
    # macd 总分40
    data = exam_macd(data)
    # 评分等级划分
    data = score_level(data)
    return data


# macd 积分系统
def exam_all(data):
    # macd 线评分
    score_info = {'score': 0, 'trend': 0, 'msg': ''}
    data['score_info'] = score_info
    data = exam_ave(data)  # 均线评分
    data = exam_macd(data)  # macd 评分
    # data = sl_monitor(data)  # 实际应用评分
    return score_level(data)


# macd 考试 40分
def exam_macd(data):
    rq = data['rq']
    code = data['code']
    rt_price = data['rt_price']
    ave5 = data['ave5']
    ave10 = data['ave10']
    # 获取指定日期前三天
    macd_list = macd_util_v1.get_macd_rt(code, 3, rq, rt_price)
    if not isinstance(macd_list, list) or len(macd_list) < 3:
        print('error,查询macd异常')
        return data
    macd_info1 = json_util.info_to_json(macd_list[0])
    macd_info2 = json_util.info_to_json(macd_list[1])
    macd_info3 = json_util.info_to_json(macd_list[2])
    dif1 = macd_info1['ema12'] - macd_info1['ema26']
    dif2 = macd_info2['ema12'] - macd_info2['ema26']
    dif3 = macd_info3['ema12'] - macd_info3['ema26']
    macd1 = macd_info1['macd']
    macd2 = macd_info2['macd']
    macd3 = macd_info3['macd']
    dea1 = macd_info1['dea']
    dea2 = macd_info2['dea']

    dif_form = form_util.thr_form([dif1, dif2, dif3])
    dif_form2 = form_util.zero_form(dif1, dif2)
    dea_form2 = form_util.zero_form(dea1, dea2)
    macd_form = form_util.form_thr(form_util.parse_x(macd_list, 'macd'))
    macd_form2 = form_util.zero_form(macd1, macd2)
    score_info = data['score_info']
    score_info['dif_form'] = dif_form
    score_info['macd_form2'] = macd_form2
    score_info['dea_form2'] = dea_form2
    score_info['dif_form2'] = dif_form2
    score_info['macd_form'] = macd_form
    data['macd'] = macd_form

    score = score_info['score']
    msg = score_info['msg']
    msg += ',macd_form' + macd_form + ',macd_form2' + macd_form2
    msg += ',dif_form' + dif_form + ',dif_form2' + dif_form2
    # macd_form ^, macd_form2L, dif_form /, dif_form2L 青铜
    # macd_form /, macd_form2L, dif_form /, dif_form2L 黄金
    # 最高分20 最低分0
    if macd_form == '/' and macd_form2 == 'T':
        if macd1 > -0.1:
            score += 20
        else:
            score += 10
    elif macd_form == '/' and macd_form2 == '/':
        score += 20
    elif macd_form == '/' and macd_form2 == 'L':
        if macd1 > 0.3:
            score += 5
        else:
            score += 10
    elif macd_form == 'v' and macd_form2 == '/':
        score += 20
    elif macd_form == 'v' and macd_form2 == 'T':
        if macd1 > -0.1:
            score += 10
        else:
            score += 5
    elif macd_form == 'v' and macd_form2 == 'L':
        if macd1 > 0.3:
            score += 5
        else:
            score += 10
    # 特殊情况1 将破未破
    elif macd_form == '^' and macd_form2 == 'L':
        if macd1 > macd3 and ave10 < rt_price < ave5:
            score += 5

    # dif 20分 最低0
    if dif_form == '/':
        if dif_form2 == 'T':
            if dif1 >= -2:
                score += 10
            else:
                score += 5
        elif dif_form2 == '/':
            score += 20
        elif dif_form2 == 'L' and dea_form2 == '/':
            score += 15
        elif dif_form2 == 'L':
            if dif1 < 1:
                score += 15
            else:
                score += 5
    elif dif_form == 'v':
        if dif_form2 == 'T':
            if dif1 >= -2:
                score += 10
            else:
                score += 5
        elif dif_form2 == '/':
            score += 20
        elif dif_form2 == 'L':
            if dif1 < 1:
                score += 15
            else:
                score += 5

    score_info['score'] = score
    score_info['msg'] = msg
    data['score_info'] = score_info
    data['total_score'] = data['total_score'] + 40
    return data


# 均线评分 50
def exam_ave(data):
    rt_price = data['rt_price']
    ave5 = data['ave5']
    ave10 = data['ave10']
    flag5 = data['flag5']
    flag10 = data['flag10']
    score_info = data['score_info']
    score = score_info['score']
    trend = score_info['trend']
    msg = score_info['msg']
    # (1) 突破十日线 and macd形态多头 全仓买入
    # (2) 突破5日均线,未突破10日均线,macd多头形态 半仓买入
    # (3) 跌破十日线 and macd形态空头 清仓卖出
    # (4) 跌破5日均线,未跌破10日线,macd空头形态 减半仓
    if rt_price > ave10:
        msg += '突破十日线'
        score += 50
        trend += 80
    elif rt_price > ave5:
        msg += '突破五日线'
        score += 25
        trend += 80
    elif rt_price < ave10:
        msg += '跌破十日线'
        score += 0
        trend -= 80
    elif rt_price < ave5:
        msg += '跌破五日线'
        score += 25
        trend -= 80
    else:
        print('无均线触发')
    score_info['score'] = score
    score_info['msg'] = msg
    data['score_info'] = score_info
    return data


# 评分等级划分
def score_level(data):
    score_info = data['score_info']
    total_score = data['total_score']
    score = score_info['score']
    if total_score == 0:
        score = 0
    else:
        score = round((score / total_score) * 100, 2)
    # 王者 星耀 钻石 铂金 黄金 白银 青铜
    if 90 < score <= 100:
        score_info['level'] = '9-国服大神'
    elif 70 < score <= 90:
        score_info['level'] = '8-荣耀王者'
    elif 70 < score <= 80:
        score_info['level'] = '7-王者'
    elif 60 < score <= 70:
        score_info['level'] = '6-星耀'
    elif 50 < score <= 60:
        score_info['level'] = '5-钻石'
    elif 40 < score <= 50:
        score_info['level'] = '4-铂金'
    elif 30 < score <= 40:
        score_info['level'] = '3-黄金'
    elif 20 < score <= 30:
        score_info['level'] = '2-白银'
    elif 10 < score <= 20:
        score_info['level'] = '1-青铜'
    elif score <= 10:
        score_info['level'] = '0-黑铁'
    return data


# 实际遇到的雷,排雷
def sl_monitor(data):
    ave_flag = data['ave_flag']
    score_info = data['score_info']
    dif_form = score_info['dif_form']
    macd_form2 = score_info['macd_form2']
    dea_form2 = score_info['dea_form2']
    dif_form2 = score_info['dif_form2']
    msg = score_info['msg']
    score = score_info['score']
    # 实例监控 买入避坑1 - 中远海控20210122
    if ave_flag == 1:
        if (macd_form2 == 'T' or macd_form2 == '\\') and (dif_form == '\\'):  # 出现过死亡交叉
            score_info['score'] = 0
            score_info['dif_form'] = score_info
    # 实例监控 买入避坑1 - 郑州煤电20210126
    return data


if __name__ == '__main__':
    exam_macd({})
